package com.server.commons.rabbitmq;

import com.server.commons.rabbitmq.model.MyCorrelationData;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import java.util.UUID;

public class MyRabbitTemplate extends RabbitTemplate{

    public MyRabbitTemplate(ConnectionFactory connectionFactory) {
        super(connectionFactory);
    }


    public void convertAndSend(MyCorrelationData myCorrelationData){

        String messageId = UUID.randomUUID().toString().replace("-","");

        MessagePostProcessor messagePostProcessor = message -> {
            MessageProperties messageProperties = new MessageProperties();
            messageProperties.setMessageId(messageId);
            messageProperties.setReceivedExchange(myCorrelationData.getExchangeName());
            messageProperties.setReceivedRoutingKey(myCorrelationData.getRoutingKey());
            return new Message(myCorrelationData.getBody().getBytes(),messageProperties);
        };

        myCorrelationData.setId(messageId);

        convertAndSend(myCorrelationData.getExchangeName(),myCorrelationData.getRoutingKey(),
                myCorrelationData.getBody(),messagePostProcessor, myCorrelationData);
    }

}
